// ##############################
// ####  note:  bokeweilai device doorbell main fun
// ####  create:  TANWENHUI (twh:谭文辉)
// ####  date:   2024/6/18
// ##############################

#include "bkwl_common.h"
#include "bkwl_http.h"
#include "bkwl_str.h"
#include "bkwl_mqtt.h"
#include "bkwl_data.h"
#include "bkwl_camera.h"
#include "cJSON.h"
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
#include "FreeRTOS.h"
#include "task.h"

void bkwl_doorbell_main()
{
}

uint64_t bkwl_doorbell_init_get_time()
{
	char *json_data = bkwl_http_get(BKWL_HTTP_GET_TIME);
	uint64_t nValue = 0;
	cJSON *root = cJSON_Parse(json_data);
	if (root != NULL)
	{
		nValue = cJSON_GetObjectItem(root, "time")->valuedouble;
		int rec = 0;
		struct timeval tv;
		tv.tv_sec = nValue;
		tprintf("the second: %lld\n", tv.tv_sec);
		tv.tv_usec = 0;
		rec = bk_rtc_settimeofday(&tv, NULL);
		if (rec < 0)
		{
			tprintf("settimeofday failed!\n");
		}
		else
		{
			tprintf("Set system time ok!\n");
		}
	}
	free(json_data);
	return nValue;
}

bk_err_t bkwl_doorbell_init_get_mqtt()
{
	bk_err_t ret = BK_OK;
	char *json_data = bkwl_http_get(BKWL_HTTP_GET_MQTT);
	cJSON *item = NULL;
	cJSON *root = cJSON_Parse(json_data);
	if (root != NULL)
	{
		item = cJSON_GetObjectItem(root, "mqttip");
		if (item != NULL)
		{
			strcpy(bkwl_main->mqtt.host, item->valuestring);
		}
		item = cJSON_GetObjectItem(root, "port");
		if (item != NULL)
		{
			bkwl_main->mqtt.port = atoi(item->valuestring);
		}
	}
	free(json_data);
	return ret;
}

bk_err_t bkwl_doorbell_init_get_license(char *company_name, uint64_t time, char *sign)
{
	bk_err_t ret = BK_OK;
	char url_send[250] = {0};
	sprintf(url_send, "%s%s&time=%lld&sign=%s", BKWL_HTTP_GET_DEVICE, company_name, time, sign);
	tprintf("url_send=%s\n", url_send);
	char *json_data = bkwl_http_get(url_send);
	cJSON *root = cJSON_Parse(json_data);
	if (root != NULL)
	{
		cJSON *child = cJSON_GetObjectItem(root, "data");
		if (child != NULL)
		{
			strcpy(bkwl_main->mqtt.device_name, cJSON_GetObjectItem(child, "device_name")->valuestring);
			tprintf("device_name=%s\n", bkwl_main->mqtt.device_name);
			strcpy(bkwl_main->mqtt.product_key, cJSON_GetObjectItem(child, "product_key")->valuestring);
			tprintf("product_key=%s\n", bkwl_main->mqtt.product_key);
			strcpy(bkwl_main->mqtt.device_secret, cJSON_GetObjectItem(child, "device_secret")->valuestring);
			tprintf("device_secret=%s\n", bkwl_main->mqtt.device_secret);
		}
	}
	free(json_data);
	return ret;
}

bk_err_t bkwl_doorbell_init_mqtt()
{
	bk_err_t ret = BK_OK;
	uint64_t cur_time = bkwl_doorbell_init_get_time();
	char sign_buf[100] = {0};

	tprintf("set mqtt get time=%lld\n", cur_time);
	sprintf(sign_buf, "time=%lld&uname=%s", cur_time, BKWL_PRODUCT_BK7258_USERNAME);
	char get_sign[33] = {0};
	bkwl_get_hmac_data(sign_buf, BKWL_PRODUCT_BK7258_SECRET, (char *)get_sign);
	bkwl_doorbell_init_get_mqtt();
	bkwl_doorbell_init_get_license(BKWL_PRODUCT_BK7258_USERNAME, cur_time, get_sign);

	tprintf("set mqtt default device data\n");

	strcpy(bkwl_main->mqtt.device_name, "D_OSdzmGueXwlln1cY");
	tprintf("default device_name=%s\n", bkwl_main->mqtt.device_name);
	strcpy(bkwl_main->mqtt.product_key, "P_1jOMUMWtsEeP8PLP");
	tprintf("default product_key=%s\n", bkwl_main->mqtt.product_key);
	strcpy(bkwl_main->mqtt.device_secret, "gXd9pmCavN8VP2qZVguPjuQZzaPtD0R0");
	tprintf("default device_secret=%s\n", bkwl_main->mqtt.device_secret);

	tprintf("bkwl_doorbell_init_mqtt product_key=%s\n", bkwl_main->mqtt.product_key);
	bkwl_data_mqtt_init(bkwl_main->mqtt.device_name, bkwl_main->mqtt.product_key);
	bkwl_mqtt_client_init(bkwl_main->mqtt.host, bkwl_main->mqtt.device_name, bkwl_main->mqtt.device_secret, bkwl_main->topic.MQTT_PROPERTY_SERVICE_SET);
	return ret;
}

bk_err_t bkwl_doorbell_post_image()
{

	bk_err_t ret = BK_OK;
	bkwl_image_t *bkwl_image = bkwl_camera_get_image();
	char *url_post_data = "http://m.yo-jia.cn/api/wx/upload/upload.php";
	uint8_t *post_data = NULL;
	uint32_t data_len = 0;
	char *device_name = NULL;
	tprintf("bkwl_doorbell_post_image  1\n");
	if (bkwl_image != NULL && bkwl_image->len > 0)
	{
		tprintf("bkwl_image not NULL\n");
		post_data = (uint8_t *)bkwl_image->buffer;
		data_len = bkwl_image->len;
		tprintf("bkwl_image data_len=%d\n", data_len);
	}
	else
	{
		tprintf("bkwl_image is NULL\n");
		post_data = (uint8_t *)"aaaaaaa";
		data_len = strlen((char *)post_data);
		tprintf("bkwl_image post_data=%s\n", (char *)post_data);
	}
	tprintf("bkwl_doorbell_post_image  2\n");
	if (bkwl_main != NULL && strlen(bkwl_main->mqtt.device_name) > 2)
	{
		tprintf("bkwl_doorbell_post_image  3\n");
		device_name = bkwl_main->mqtt.device_name;
		tprintf("bkwl_doorbell_post_image  4\n");
	}
	else
	{
		device_name = "bbbbb";
	}

	tprintf("bkwl_doorbell_post_image  4 ,device_name=%s\n", device_name);
	bkwl_http_post_filedata(url_post_data, post_data, data_len, device_name);
	return ret;
}
